﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class SchoolInfoController : Controller
    {
        public ActionResult SchoolInfoList()
        {
            return View();
        }
        //查询
        public ActionResult List(View_BD_SchoolInfo SchoolInfo)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<View_BD_SchoolInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_SchoolInfo>(SchoolInfo, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.View_BD_SchoolInfoBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.View_BD_SchoolInfoBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddSchoolInfo(BD_SchoolInfo DD)
        {
            var list1 = DALSession.Current.BD_SchoolInfoDAL.GetListBy(d => d.SchoolCode == DD.SchoolCode && d.DelState == "0");
            if (list1.Count == 0)
            {
                //生成校区编号
                var list2 = DALSession.Current.BD_SchoolInfoDAL.GetListBy();
                var NewSchoolCode = (list2.Count + 1).ToString("000");

                DD.SchoolCode = NewSchoolCode;
                DD.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                DD.EditPerName = OperateContext.sessUserInfo.Name;
                DD.EditPerID = OperateContext.sessUserInfo.Id;
                DD.DelState = "0";
                BLLSession.Current.BD_SchoolInfoBLL.Add(DD);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditSchoolInfo(BD_SchoolInfo DD)
        {
            DD.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            DD.EditPerName = OperateContext.sessUserInfo.Name;
            DD.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_SchoolInfoBLL.ModifyBy(DD, t => t.ID == DD.ID, "SchoolName", "IfHeadquarters", "SchoolAddress", "EditPerID", "EditPerName", "EditTime", "Note", "DelState", "ParentId");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelSchoolInfo(int Id)
        {
            //BLLSession.Current.BD_SchoolInfoBLL.DelBy(d => d.ID == Id);//直接删除
            BD_SchoolInfo BD_SchoolInfo1 = DALSession.Current.BD_SchoolInfoDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_SchoolInfo1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_SchoolInfo1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_SchoolInfo1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_SchoolInfo1.DelState = "1";
            BLLSession.Current.BD_SchoolInfoBLL.ModifyBy(BD_SchoolInfo1, t => t.ID == BD_SchoolInfo1.ID, "SchoolCode", "SchoolName", "SchoolAddress", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
        //获取所有校区信息(返回code和name）
        public void GetAllSchoolInfo()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_SchoolInfoDAL.GetListBy(t => t.DelState == "0").Select(t => new { value = t.SchoolCode, text = t.SchoolName })));
        }
        //获取当前用户的校区及其子校区并返回tree
        public void GetOwnSchoolInfoTree()
        {
            //int roleId = Commons.String2Int32(Request.Form["roleId"]);
            //List<TreeNode> listNodes = new List<TreeNode>();
            //var showList = BLLSession.Current.BD_PermissionBLL.GetListBy(p => p.IsShow == "0");
            //LoadTreeNode(showList, listNodes, 0, roleId);
            //return Json(listNodes);
            string schoolcode = OperateContext.sessUserInfo.SchoolCode;
            List<BD_SchoolInfo> topschool = DALSession.Current.BD_SchoolInfoDAL.GetListBy(u => u.SchoolCode == schoolcode && u.DelState == "0");
            List<TreeNode> listNodes = new List<TreeNode>();
            LoadTreeNode(topschool, listNodes, 0);
            Response.Write(JsonHelper.toJson(listNodes));
        }
        private void LoadTreeNode(List<BD_SchoolInfo> topschool, List<TreeNode> listNodes, int parentid)
        {
            //bool hasright = BLLSession.Current.BD_RolePermissionBLL.isExists(u => u.RId == roleId && u.PId == currentpid);
            for (int i = 0; i < topschool.Count; i++)
            {
                BD_SchoolInfo current = topschool[i];
                TreeNode node = new TreeNode()
                {
                    id = current.ID,
                    text = current.SchoolName,
                    Checked = false,
                    attributes = new { isParent = current.IfHeadquarters },
                    children = new List<TreeNode>()
                };
                List<BD_SchoolInfo> childschool = DALSession.Current.BD_SchoolInfoDAL.GetListBy(u => u.ParentId == node.id && u.DelState == "0");
                if (current.IfHeadquarters == "1" && childschool.Count > 0)
                {
                    node.state = "closed";
                }
                listNodes.Add(node);
                if (current.IfHeadquarters == "1" && childschool.Count > 0)
                {
                    LoadTreeNode(childschool, node.children, node.id);
                }
            }

        }
        //获取所有校区信息(返回Id和name）
        public void GetAllSchoolInfoId()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_SchoolInfoDAL.GetListBy(t => t.DelState == "0").Select(t => new { value = t.ID, text = t.SchoolName })));
        }
        //根据Code获取校区名称
        public void GetSchoolName()
        {
            string SchoolCode2 = Request.Form["SchoolCode"];
            var con = DALSession.Current.BD_SchoolInfoDAL.GetListBy(c => c.SchoolCode == SchoolCode2);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { SchoolCode = SchoolCode2, SchoolName = SchoolCode2 }));
            }
        }
        //根据Id获取校区名称
        public void GetSchoolNameById()
        {
            int SchoolId = Commons.String2Int32(Request.Form["SchoolId"]);
            var con = DALSession.Current.BD_SchoolInfoDAL.GetListBy(c => c.ID == SchoolId);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { SchoolId = SchoolId, SchoolName = SchoolId }));
            }
        }

        public JsonResult GetParentSchool()
        {
            var list = DALSession.Current.BD_SchoolInfoDAL.GetListBy(p => p.IfHeadquarters == "1"&&p.DelState=="0").Select(p => new { value = p.ID, text = p.SchoolName });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}
